<?php
include_once "src/controller/regmanagement/AccountManager.php";
include_once "src/model/Consultant.php";
include_once "src/model/ProvidedService.php";
include_once "src/model/SearchResult.php";

try {
    
    if (isset($_POST["paginationGoToPage"])) {
        $servicesList = array();

        for ($i = 0; $i < $_POST["paginationServicesSelectedCount"]; $i++) {
            if ($_POST["paginationService" . $i] != "false") {
                $servicesList[$i] = $_POST["paginationService" . $i];
            } else {
                $servicesList[$i] = "false";
            }
        }

        $searchResult = AccountManager::searchConsultants($_POST["paginationSearchIndicator"], $_POST["paginationConsultantName"], $_POST["paginationEmail"], $_POST["paginationCompanyName"], $servicesList,  $_POST["paginationGoToPage"]);
        $users = $searchResult->getConsultants();
        
    } else {
        $servicesList = array();

        for ($i = 0; $i < $_POST["servicesSelectedCount"]; $i++) {
            if ($_POST["service" . $i] != "false") {
                $servicesList[$i] = $_POST["service" . $i];
            } else {
                $servicesList[$i] = "false";
            }
        }
        //Print_r ($servicesList);
        $searchResult = AccountManager::searchConsultants($_POST["searchIndicator"], $_POST["consultantName"], $_POST["email"], $_POST["companyName"], $servicesList,  $_POST["pageNumberRequested"]);
        $users = $searchResult->getConsultants();
        
    }

?>
{
    "identifier": "id",
    "label": "consultantName",
    "totalNumberResults": "<?php echo $searchResult->getTotalNumberResults(); ?>",
    "totalNumberPageResults": "<?php echo count($users); ?>",
    "pageNumber": "<?php echo $searchResult->getPageNumber(); ?>",
    "items": [
<?php
    for ($i = 0; $i < count($users); $i++) {
        $tempUser = $users[$i];
        if ($i == 0) {
            echo '{ "id":"' . $tempUser->getConsultantId() . '", "consultantName":"' . $tempUser->getProfileLastName() . ", " . $tempUser->getProfileFirstName() . '", "companyName":"' . $tempUser->getCompanyName() . '", "telephone":"' . $tempUser->getTelephone() . '", "email":"' . $tempUser->getProfileEmail() . '", "accountEmail":"' . $tempUser->getEmail() . '", "website":"' . $tempUser->getWebsite() . '" }';
        } else {
            echo ', { "id":"' . $tempUser->getConsultantId() . '", "consultantName":"' . $tempUser->getProfileLastName() . ", " . $tempUser->getProfileFirstName() . '", "companyName":"' . $tempUser->getCompanyName() . '", "telephone":"' . $tempUser->getTelephone() . '", "email":"' . $tempUser->getProfileEmail() . '", "accountEmail":"' . $tempUser->getEmail() . '", "website":"' . $tempUser->getWebsite() . '" }';
        }
    }
    
} catch (Exception $e) {
    //echo '{ id:"0" }';
}
//sleep(3);
?>
    ]
}